package org.jeecg.modules.talent.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.talent.entity.Talent;
import org.jeecg.modules.talent.Utils.PageUtils;
import org.jeecg.modules.talent.service.ITalentService;
import org.jeecg.modules.talent.service.impl.TalentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping("/talent/talent")
@ResponseBody
public class TalentController extends JeecgController<Talent, ITalentService> {
    @Autowired
    private TalentServiceImpl talentService;

    @GetMapping("/list")
    public Result<?> queryPageList(Talent talent,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<Talent> list=talentService.findAll(talent);
        PageInfo<Talent> pageInfo=new PageInfo<>(list);
        PageUtils pageUtils=new PageUtils(pageInfo);
        return Result.ok(pageUtils);
    }

    /**
     * 添加
     */
    @PostMapping("/add")
    public Result<?> add(@RequestBody Talent talent){
        talentService.insertTalent(talent);
        return Result.ok("添加成功!");
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public Result<?> edit(@RequestBody Talent talent) {
        talentService.updateTalent(talent);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     */
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) int id) {
        talentService.deleteTalentById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.talentService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) int id) {
        Talent talent = talentService.selectById(id);
        return Result.ok(talent);
    }

    /**
     * 导入excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Talent talent) {
        return super.exportXls(request, talent, Talent.class, "人才信息管理");
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Talent.class);
    }
}
